package com.memorypalace.study.biz;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.memorypalace.deck.mapper.CardMapper;
import com.memorypalace.pojo.Card;
import com.memorypalace.pojo.CardStudyRecord;
import com.memorypalace.pojo.StudyList;
import com.memorypalace.study.mapper.CardStudyRecordMapper;


@Service("csrBiz")
public class CardStudyRecordBizImpl implements ICardStudyRecordBiz {

	@Autowired
	private CardStudyRecordMapper csrMapper;
	@Autowired
	private CardMapper cardMapper;

	@Override
	public Boolean insertCardStudyRecord(CardStudyRecord cardStudyRecord) {
		Card card = cardMapper.getCardByCardId(cardStudyRecord.getCardId());
		if(card.getCardStudyCount() == 0){
			cardStudyRecord.setIsNew(true);
		}else{
			cardStudyRecord.setIsNew(false);
		}
		card.setCardStudyCount(card.getCardStudyCount() + 1);
		if (cardStudyRecord.getAnswerButton() == 1) {
			card.setCardWrongCount(card.getCardWrongCount() + 1);
		}
		long now = System.currentTimeMillis() + cardStudyRecord.getStudyInterval().getDay() * 24 * 60 * 60 * 1000
				+ cardStudyRecord.getStudyInterval().getMinutes() * 60 * 1000;

	
		card.setCardNextStudyTime(new Date(now));
		// 下次学习时间=当前学习时间+间隔

		
		cardMapper.updateCardByCardId(card);

		return csrMapper.insertCardStudyRecord(cardStudyRecord);
	}

	

	@Override
	public Integer queryStudySequence(String studySchemeId) {

		return csrMapper.queryStudySequence(studySchemeId);

	}



	@Override
	public Integer getTodayNewCardStudyCount(StudyList studyList) {
		// TODO Auto-generated method stub
		return csrMapper.getTodayNewCardStudyCount(studyList);
	}



	@Override
	public Integer getTodayReviewCardStudyCount(StudyList studyList) {
		// TODO Auto-generated method stub
		return csrMapper.getTodayReviewCardStudyCount(studyList);
	}

}
